package com.ruoyi.boat.controller;

import com.ruoyi.boat.aop.AvoidDuplicate;
import com.ruoyi.boat.domain.Alert;
import com.ruoyi.boat.domain.BoatUser;
import com.ruoyi.boat.domain.Device;
import com.ruoyi.boat.domain.DeviceBoat;
import com.ruoyi.boat.dto.AlertBoatDto;
import com.ruoyi.boat.dto.BoatUserDto;
import com.ruoyi.boat.dto.DeviceBoatDto;
import com.ruoyi.boat.service.IAlertService;
import com.ruoyi.boat.service.IBoatService;
import com.ruoyi.boat.service.IBoatUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@Controller
@RequestMapping("/boat/alert")
public class AlertController extends BaseController {
    private String prefix = "boat/alert";

    @Autowired
    private IAlertService alertService;

    @Autowired
    private IBoatService boatService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IBoatUserService boatUserService;

    @RequiresPermissions("boat:alert:view")
    @GetMapping()
    public String alert() {
        return prefix + "/manage";
    }

    /**
     * 查询告警列表
     */
    @RequiresPermissions("boat:alert:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Alert alert) {
        startPage();
        List<Alert> list = alertService.selectAlertList(alert);
        return getDataTable(list);
    }

    /**
     * 查询告警列表
     */
    @RequiresPermissions("boat:alert:list")
    @GetMapping("/get")
    @ResponseBody
    public TableDataInfo get(Long boatId) {
        List<Alert> list = alertService.selectAlertList(new Alert().setBoatId(boatId));
        return getDataTable(list);
    }

    /**
     * 导出告警列表
     */
    @RequiresPermissions("boat:alert:export")
    @Log(title = "告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AvoidDuplicate
    @ResponseBody
    public AjaxResult export(Alert alert) {
        List<Alert> list = alertService.selectAlertList(alert);
        ExcelUtil<Alert> util = new ExcelUtil<Alert>(Alert.class);
        return util.exportExcel(list, "告警数据");
    }

    /**
     * 新增告警
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存告警
     */
    @RequiresPermissions("boat:alert:add")
    @Log(title = "告警", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Alert alert) {
        return toAjax(alertService.insertAlert(alert));
    }

    /**
     * 修改告警
     */
    @RequiresPermissions("boat:alert:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Alert alert = alertService.selectAlertById(id);
        mmap.put("alert", alert);
        return prefix + "/edit";
    }

    /**
     * 修改保存告警
     */
    @RequiresPermissions("boat:alert:edit")
    @Log(title = "告警", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Alert alert) {
        return toAjax(alertService.updateAlert(alert));
    }

    /**
     * 删除告警
     */
    @RequiresPermissions("boat:alert:remove")
    @Log(title = "告警", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(alertService.deleteAlertByIds(ids));
    }


    /**
     * 报警设置界面
     *
     * @param mmap
     * @return
     */
    @RequiresPermissions("boat:alert:edit")
    @GetMapping("/set/{boatId}")
    public String set(ModelMap mmap, @PathVariable Long boatId) {
        mmap.put("boat", boatService.selectBoatById(boatId));
        mmap.put("alertList", alertService.selectAlertList(new Alert().setBoatId(boatId)));
        return "/boat/alert/set";
    }

    /**
     * 保存报警设置
     */
    @RequiresPermissions("boat:alert:edit")
//    @Log(title = "波浪艇", businessType = BusinessType.INSERT)
    @PostMapping("/set")
    @ResponseBody
    public AjaxResult set(@RequestBody AlertBoatDto alertBoatDto) {
        alertService.deleteByBoatId(alertBoatDto.getBoatId());
        for (Alert alert : alertBoatDto.getAlerts()) {
            alertService.insertAlert(alert);
        }
        return AjaxResult.success();
    }


    /**
     * 分配用户界面
     *
     * @param mmap
     * @return
     */
    @RequiresPermissions("boat:alert:edit")
    @GetMapping("/user/{boatId}")
    public String user(ModelMap mmap, @PathVariable Long boatId) {
        mmap.put("boat", boatService.selectBoatById(boatId));
        mmap.put("userList", userService.selectUserList(new SysUser()));
        List<Long> userIdList = boatUserService.selectBoatUserList(new BoatUser().setBoatId(boatId)).stream().map(BoatUser::getUserId).collect(Collectors.toList());
        mmap.put("userIdList", userIdList);
        return "/boat/alert/user";
    }

    /**
     * 保存用户分配
     */
    @RequiresPermissions("boat:alert:edit")
//    @Log(title = "波浪艇", businessType = BusinessType.INSERT)
    @PostMapping("/user")
    @ResponseBody
    public AjaxResult user(BoatUserDto boatUserDto) {
        Long boatId = boatUserDto.getBoatId();
        List<Long> users = boatUserDto.getUsers();
        boatUserService.deleteBoatUserByBoatId(boatId);
        if (users != null && !users.isEmpty()) {
            for (Long userId : users) {
                boatUserService.insertBoatUser(new BoatUser().setBoatId(boatId).setUserId(userId));
            }
        }
        return AjaxResult.success();
    }

}
